﻿
Public Class CommandLine

    'As neither "Environment.CommandLine.Trim" nor "My.Application.CommandLineArgs" can handle strings enclosed by quotes (")

    Private Shared mcolArguments As Specialized.StringCollection

    Shared Sub New()
        CreateArgsCollection()
    End Sub

    Shared ReadOnly Property Args As Specialized.StringCollection
        Get
            Return mcolArguments
        End Get
    End Property

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Shared Shadows ReadOnly Property ToString As String
        Get
            Return System.Environment.CommandLine.Trim
        End Get
    End Property

    ''' <summary>
    ''' Check if argument is in command line (case insensitive)
    ''' </summary>
    ''' <param name="TextValue">Argument to search for</param>
    ''' <returns>True if found, false if not</returns>
    ''' <remarks></remarks>
    Shared Function ContainsArgument(ByVal TextValue As String) As Boolean
        Dim Arg As String
        Dim Posi As Integer
        'Dim k As Short

        For Each Arg In mcolArguments
            'k = +1
            'Console.WriteLine(k & ":" & Arg.ToUpper)
            Posi = InStr(Arg, ":")
            If Posi > 0 Then
                If TextValue.ToUpper.Replace("/", "") = Arg.Substring(0, Posi - 1).ToUpper.Replace("/", "") Then
                    Return True
                    Exit Function
                End If
            End If

            Posi = InStr(Arg, "=")
            If Posi > 0 Then
                If TextValue.ToUpper.Replace("/", "") = Arg.Substring(0, Posi - 1).ToUpper.Replace("/", "") Then
                    Return True
                    Exit Function
                End If
            End If

            If TextValue.ToUpper = Arg.ToUpper Then
                'Console.WriteLine(" found it!")
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function

    ''' <summary>
    ''' Splits one argument into Key and Value (separated by colon) and returns the value part of the argument that matches the KeyString
    ''' </summary>
    ''' <param name="KeyString">Key part of argument</param>
    ''' <returns>Value part of the argument</returns>
    ''' <remarks></remarks>
    Shared Function GetCommandlineArgumentValue(ByVal KeyString As String) As String
        '/KEY:VALUE, example /LOGSIZE:1000000
        Dim Arg As String, Key As String, Posi As Integer
        For Each Arg In mcolArguments
            Posi = InStr(Arg, ":")
            If Posi > 0 Then
                Key = Arg.Substring(0, Posi - 1)
                If KeyString.ToUpper.Replace("/", "") = Key.ToUpper.Replace("/", "") Then
                    Return Arg.ToString.Substring(Posi)
                    Exit Function
                End If
            End If
            Posi = InStr(Arg, "=")
            If Posi > 0 Then
                Key = Arg.Substring(0, Posi - 1)
                If KeyString.ToUpper.Replace("/", "") = Key.ToUpper.Replace("/", "") Then
                    Return Arg.ToString.Substring(Posi)
                    Exit Function
                End If
            End If
        Next
        Return ""
    End Function

    'Better replacement of --> My.Application.CommandLineArgs as the standard one can't handle strings enclosed in Quotes (")
    Private Shared Sub CreateArgsCollection()
        Dim value As String = Environment.CommandLine.Trim
        Dim bInsideQuote As Boolean
        Dim iCnt As Integer

        mcolArguments = New Specialized.StringCollection

        If value = "" Then
            mcolArguments = Nothing
            Exit Sub
        End If

        For Each c As Char In value.ToCharArray
            iCnt += 1
            If c = """"c Then
                bInsideQuote = Not bInsideQuote
                Continue For
            End If
            If c = " " Then
                If bInsideQuote Then
                    Continue For
                Else
                    Mid$(value, iCnt, 1) = "§"
                End If
            End If
        Next
        'Replace multiple separators
        While value.IndexOf("§§") > 0
            value = value.Replace("§§", "§")
        End While
        Dim CmdArgs As New List(Of String)
        Dim FirstArg As Boolean = True
        For Each sArg As String In value.Split("§".ToCharArray)
            If FirstArg Then
                FirstArg = False
                Continue For
            End If
            'CmdArgs.Add(RemoveQuote(sArg))
            mcolArguments.Add(sArg.Replace("""", ""))
        Next
    End Sub
End Class
